package com.ruoyi.framework.config;

import com.kfyty.loveqq.framework.core.autoconfig.annotation.Bean;
import com.kfyty.loveqq.framework.core.autoconfig.annotation.Configuration;
import com.kfyty.loveqq.framework.core.autoconfig.annotation.Value;
import com.kfyty.loveqq.framework.core.autoconfig.condition.annotation.ConditionalOnProperty;
import com.kfyty.loveqq.framework.web.mvc.servlet.FilterRegistrationBean;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.xss.XssFilter;

import java.util.Arrays;

/**
 * Filter配置
 *
 * @author ruoyi
 */
@Configuration
@ConditionalOnProperty(value = "xss.enabled", havingValue = "true")
public class FilterConfig
{
    @Value("${xss.excludes}")
    private String excludes;

    @Value("${xss.urlPatterns}")
    private String urlPatterns;

    @Bean(resolveNested = false, independent = true)
    public FilterRegistrationBean xssFilterRegistration()
    {
        FilterRegistrationBean registration = new FilterRegistrationBean();
        registration.setFilter(new XssFilter());
        registration.setUrlPatterns(Arrays.asList(StringUtils.split(urlPatterns, ",")));
        registration.setFilterName("xssFilter");
        registration.addInitParam("excludes", excludes);
        return registration;
    }
}
